portable getline replacement (closes #182)
authorSteven G. Johnson <stevenj@alum.mit.edu>
Sat, 28 Mar 2020 13:36:58 +0000 (09:36 -0400)
committerSteven G. Johnson <stevenj@alum.mit.edu>
Sat, 28 Mar 2020 13:36:58 +0000 (09:36 -0400)
test/graphemetest.c
test/normtest.c
test/simple_getline.h [new file with mode: 0644]

index e24ef29c106f09ef01dd8ac9063f53621c4edb4b..bc55b16cf40f5612e48ea4bd22e8fc90187ff73e 100644 (file)
@@ -1,15 +1,16 @@
 #include "tests.h"
+#include "simple_getline.h"
 
 int main(int argc, char **argv)
 {
-    char *buf = NULL;
+    char buf[8192];
     size_t bufsize = 0;
     FILE *f = argc > 1 ? fopen(argv[1], "r") : NULL;
     utf8proc_uint8_t src[1024];
     int len;
 
     check(f != NULL, "error opening GraphemeBreakTest.txt");
-    while (getline(&buf, &bufsize, f) > 0) {
+    while (simple_getline(buf, f) > 0) {
         size_t bi = 0, si = 0;
         lineno += 1;
 
index 555c14c84bfe5b4e63a8bbd93626a3b7da22925a..34921bf522b778370117fd4e6927fc11e5ffa7de 100644 (file)
@@ -1,4 +1,5 @@
 #include "tests.h"
+#include "simple_getline.h"
 
 #define CHECK_NORM(NRM, norm, src) {                                 \
     char *src_norm = (char*) utf8proc_ ## NRM((utf8proc_uint8_t*) src);      \
 
 int main(int argc, char **argv)
 {
-     char *buf = NULL;
-     size_t bufsize = 0;
+     char buf[8192];
      FILE *f = argc > 1 ? fopen(argv[1], "r") : NULL;
      char source[1024], NFC[1024], NFD[1024], NFKC[1024], NFKD[1024];
 
      check(f != NULL, "error opening NormalizationTest.txt");
-     while (getline(&buf, &bufsize, f) > 0) {
+     while (simple_getline(buf, f) > 0) {
           size_t offset;
           lineno += 1;
 
diff --git a/test/simple_getline.h b/test/simple_getline.h
new file mode 100644 (file)
index 0000000..453cff6
--- /dev/null
@@ -0,0 +1,11 @@
+/* simplistic, portable replacement for getline, sufficient for our tests */
+static size_t simple_getline(char buf[8192], FILE *f) {
+    size_t i = 0;
+    while (i < 1023) {
+        int c = getc(f);
+        if (c == EOF || c == '\n') break;
+        buf[i++] = (char) c;
+    }
+    buf[i] = 0;
+    return i;
+}